Terrorism has been a constant hindrance on mankind’s journey to achieve global peace and prosperity. From hostage situations and hijackings to mass shootings and bombings, terrorist attacks have a profound impact on both the victims and the larger society; they cause physical harm and loss of life, as well as emotional trauma and psychological distress. Needless to say, they can have long-lasting socio-economic consequences, disrupting trade and commerce, causing job losses, and decreasing investor confidence.
As the frequency of terrorist attacks are increasing at a rate faster than ever, it is crucial to understand them and their trends and patterns. In this blog post, I will be examining various aspects of terrorism including regions, targets, methods and motives using three open-source datasets: Global Terrorism Database, which contains information on over 180,000 global terrorist attacks from 1970 to 2017; World GDP dataset, which includes the GDP per Capita of different countries from 1960 to 2021; and the World Population dataset, which provides the data on fertility rate and net migration of each countries from 1955 to 2020. I hope this will shed some light on this phenomenon of global terrorism and will equip us better to combat them in the future. So lets roll up our sleeves and demystify the data from the world of global terrorism.
result_df = pd.read_csv("../results/results.csv")result_df = result_df.sort_values(by=['Root Mean Squared Error'])matplotlib.rc_file_defaults()ax1 = sns.set_style(style=None, rc=None)fig, ax1 = plt.subplots(figsize=(12,6))colors = ["#5D3FD3", "#5D3FD3", "#5D3FD3","#5D3FD3", "#0096FF", "#0096FF", "#0096FF"]sns.barplot(data = result_df, x='Model', y='Root Mean Squared Error', alpha=0.5, ax=ax1, palette=colors)ax1.set_xticklabels(ax1.get_xticklabels(), fontsize=12)ax1.set_xlabel("Models", fontsize=14)ax1.set_ylabel("Root Mean Squared Error", fontsize=14)ax1.set_title("Efficiency of Models", fontsize=16)ax2 = ax1.twinx()ax2.set_ylabel("Time (in seconds)", fontsize=14)dl = mpatches.Patch(color="#5D3FD3")ml = mpatches.Patch(color="#0096FF")custom_line = [Line2D([0], [0], color='#0096FF', lw=2), dl, ml]leg = plt.legend(custom_line, ["Time", "DL Models", "ML Models"], loc="upper left")for index, lh inenumerate(leg.legendHandles): if index >0: lh.set_alpha(0.5)sns.lineplot(data =list(result_df["Time (in seconds)"]), marker='o', ax=ax2, color='#0096FF')plt.show()
Figure 10: Efficiency of Models
Welcome to mysuper SUPER awesome blog post! \[x^2 = 1\]
Quarto is cool
This section was copy/pasted from various parts of the Quarto website.
Note
Note that there are five types of callouts, including: note, tip, warning, caution, and important.
Tip With Caption
This is an example of a callout with a caption.
For your reference, here’s an example of a Python code cell in Quarto, along with a figure that gets generated, along with a caption and a label so that it can be referred to automatically as “Figure 1” (or whatever) in the writeup.
For a demonstration of a line plot on a polar axis, see Figure 11.
Here’s an example of citing a source (see Phillips 1999, 33–35). Be sure the source information is entered in “BibTeX” form in the references.bib file.
The bibliography will automatically get generated. Any sources you cite in the document will be included. Other entries in the .bib file will not be included.
References
Phillips, T. P. 1999. “Possible Influence of the Magnetosphere on American History.”J. Oddball Res. 98: 1000–1003.
Source Code
---title: "The State of Global Terrorism"subtitle: "An In-Depth Analysis of Trends and Threats"author: "Shreehar Joshi"bibliography: references.bibnumber-sections: falseformat: html: theme: cosmo rendering: embed-resources code-fold: true code-tools: true pdf: defaultjupyter: python3---Terrorism has been a constant hindrance on mankind's journey to achieve global peace and prosperity. From hostage situations and hijackings to mass shootings and bombings, terrorist attacks have a profound impact on both the victims and the larger society; they cause physical harm and loss of life, as well as emotional trauma and psychological distress. Needless to say, they can have long-lasting socio-economic consequences, disrupting trade and commerce, causing job losses, and decreasing investor confidence.As the frequency of terrorist attacks are increasing at a rate faster than ever, it is crucial to understand them and their trends and patterns. In this blog post, I will be examining various aspects of terrorism including regions, targets, methods and motives using three open-source datasets: Global Terrorism Database, which contains information on over 180,000 global terrorist attacks from 1970 to 2017; World GDP dataset, which includes the GDP per Capita of different countries from 1960 to 2021; and the World Population dataset, which provides the data on fertility rate and net migration of each countries from 1955 to 2020. I hope this will shed some light on this phenomenon of global terrorism and will equip us better to combat them in the future. So lets roll up our sleeves and demystify the data from the world of global terrorism.```{python echo=FALSE}#| label: fig-import#| fig-cap: "Global Terrorist Attacks"import pandas as pdimport numpy as npimport plotly.express as pximport nltkfrom sklearn.metrics import mean_squared_errorfrom sklearn.ensemble import RandomForestRegressorfrom sklearn.tree import DecisionTreeRegressorfrom sklearn import neighborsimport tensorflow as tffrom PIL import Imagefrom tensorflow.keras.models import Sequentialfrom sklearn.model_selection import train_test_splitfrom tensorflow.keras.layers import Dense, Dropout, Conv1D, MaxPooling1D, Flatten, LSTM, SimpleRNNfrom tensorflow.keras.layers import Bidirectional, GRU, UpSampling1Dimport plotly.express as pxfrom sklearn.preprocessing import LabelEncoderfrom wordcloud import WordCloudimport matplotlib.pyplot as pltimport matplotlibimport matplotlib.pyplot as pltimport seaborn as snsfrom matplotlib.lines import Line2Dimport matplotlib.patches as mpatchesimport timeimport warningswarnings.filterwarnings("ignore", category=FutureWarning)df_attacks = pd.read_csv("../data/globalterrorismdb_0718dist.csv", encoding="ISO-8859-1", low_memory=False)df_attacks.head()df_attacks = df_attacks[['eventid','iyear', 'imonth', 'iday', 'country_txt', 'region_txt', 'provstate', 'city', 'latitude', 'longitude', 'suicide', 'attacktype1_txt', 'targtype1_txt', 'gname', 'motive', 'weaptype1_txt', 'nkill']]df_attacks.rename(columns={"eventid": "Event ID", "iyear": "Year", "imonth": "Month", "country_txt": "Country", "region_txt": "Region", "provstate": "Province/State", "city": "City", "latitude": "Latitude", "longitude": "Longitude", "suicide": "Suicide", "attacktype1_txt": "Attack Type","targtype1_txt": "Target Type", "gname": "Terrorist Group", "motive": "Motive", "weaptype1_txt": "Weapon Type", "nkill": "Casualties"}, inplace=True)df_population = pd.read_csv("../data/population.csv")df_population = df_population[["Country","Year", "Migrants(net)", "FertilityRate"]]df_population.rename(columns= {"FertilityRate": "Fertility Rate", "Migrants(net)": "Migrants (net)"}, inplace=True)df_gdp = pd.read_csv("../data/world_country_gdp_usd.csv")df_gdp = df_gdp[['Country Name','year', 'GDP_USD']]df_gdp.rename(columns= {"Country Name": "Country", "year": "Year", "GDP_USD":"GDP (in USD)", "GDP_per_capita_USD": "GDP (per capita)"}, inplace=True)df_us_population = pd.read_csv("../data/us_population.csv")df_us_population = df_us_population[["state", "pop2022"]]df_us_population.rename(columns= {"state": "State", "pop2022": "Population"}, inplace=True) fig = px.scatter_geo(df_attacks, lon="Longitude", lat="Latitude", animation_frame="Year", color="Region", projection="equirectangular", animation_group="Year", title="Terrorist Attacks (1970 - 2017)")fig.update_layout(title_x=0.44)fig.show()``````{python}#| label: fig-us#| fig-cap: "Terrorist Attacks in the US"us_states = np.asarray(['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA','HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA','MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY','NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX','UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'])us_state_to_abbrev = {"Alabama": "AL","Alaska": "AK","Arizona": "AZ","Arkansas": "AR","California": "CA","Colorado": "CO","Connecticut": "CT","Delaware": "DE","Florida": "FL","Georgia": "GA","Hawaii": "HI","Idaho": "ID","Illinois": "IL","Indiana": "IN","Iowa": "IA","Kansas": "KS","Kentucky": "KY","Louisiana": "LA","Maine": "ME","Maryland": "MD","Massachusetts": "MA","Michigan": "MI","Minnesota": "MN","Mississippi": "MS","Missouri": "MO","Montana": "MT","Nebraska": "NE","Nevada": "NV","New Hampshire": "NH","New Jersey": "NJ","New Mexico": "NM","New York": "NY","North Carolina": "NC","North Dakota": "ND","Ohio": "OH","Oklahoma": "OK","Oregon": "OR","Pennsylvania": "PA","Rhode Island": "RI","South Carolina": "SC","South Dakota": "SD","Tennessee": "TN","Texas": "TX","Utah": "UT","Vermont": "VT","Virginia": "VA","Washington": "WA","West Virginia": "WV","Wisconsin": "WI","Wyoming": "WY","District of Columbia": "DC","American Samoa": "AS","Guam": "GU","Northern Mariana Islands": "MP","Puerto Rico": "PR","United States Minor Outlying Islands": "UM","U.S. Virgin Islands": "VI",}df_attacks_us = df_attacks[df_attacks["Country"] =="United States"] df_attacks_us = pd.DataFrame(df_attacks_us.groupby("Province/State")["Event ID"].count())df_attacks_us = df_attacks_us.reset_index()df_attacks_us.rename(columns={"Province/State": "State", "Event ID": "Number of Terrorist Attacks"}, inplace=True)df_attacks_us = df_attacks_us[df_attacks_us["State"] !="Unknown"]df_attacks_us["State Code"] = df_attacks_us["State"].apply(lambda x: us_state_to_abbrev[x])def scale_column(df, column, minVal=float('-inf'), maxVal=float('inf')):if minVal ==float('-inf'): minVal =min(df[column])if maxVal ==float('inf'): maxVal =max(df[column]) res = []for val in df[column]: res.append((val - minVal) / (maxVal - minVal))return resdf_us_population.head()df_attacks_us = df_attacks_us.merge(df_us_population[['State', 'Population']])df_attacks_us["Number of Terrorist Attacks (Standardised)"] = df_attacks_us["Number of Terrorist Attacks"] / df_attacks_us["Population"]tempVal = scale_column(df_attacks_us, "Number of Terrorist Attacks (Standardised)")df_attacks_us["Number of Terrorist Attacks (Standardised)"] = tempValdf_attacks_us = df_attacks_us.sort_values(by="Number of Terrorist Attacks (Standardised)", ascending=False)fig = px.choropleth(df_attacks_us, locations='State Code', color='Number of Terrorist Attacks (Standardised)', color_continuous_scale="Viridis", locationmode="USA-states", scope="usa", labels={'Number of Terrorist Attacks (Standardised)':'No. of Terrorist Attacks'}, title="Terrorist Attacks in the US (1970-2017)")fig.update_layout(title_x=0.44)fig.update_layout( legend = {"xanchor": "right", "x": -0, "y":1.9})fig.update_layout(height=500, width=780)fig.show()``````{python}#| label: fig-motives#| layout-ncol: 2#| fig-cap: "Attack Motives"#| fig-subcap: #| - "1970-2000"#| - "2000-2017"stpwrd = nltk.corpus.stopwords.words('english')extended_list = ["specific", "motive", "unknown", "Unknown", "incident", "claimed", "responsibility", "however", "unaffiliated", "individual", "identified", "killed", "stated", "anti", "attacks", "protest", "carried", "attack", "trend", "larger", "may", "part", "following", "community", "sources", "violence", "targeting", "noted", "posited", "suspected", "targeting", "members", "noted", "targeted", "also", "assailant", "perpetrator", "meant", "bring attention", "practice", "perpetrator", "assailant", "meant", "bring", "attention"]stpwrd.extend(extended_list)df_attacks_us = df_attacks[df_attacks["Country"] =="United States"]df_attacks_us = df_attacks_us[["Year", "Motive"]]df_attacks_us = df_attacks_us.dropna()temp_df = df_attacks_us[(df_attacks_us["Year"] >=1970) & (df_attacks_us["Year"] < (2000))]motive =list(temp_df["Motive"].values)motive =" ".join(motive)wordcloud = WordCloud(width=1000, height=800, background_color ='white', stopwords=stpwrd, color_func=lambda*args, **kwargs: "green", min_font_size =10).generate(motive)plt.figure(figsize = (12, 12), facecolor =None) plt.imshow(wordcloud) plt.axis("off")plt.tight_layout(pad =2)plt.title("Attack Motives ("+str(1970) +" - "+str(2000) +")", fontdict={'fontsize': 36})plt.show()stpwrd = nltk.corpus.stopwords.words('english')stpwrd.extend(extended_list)df_attacks_us = df_attacks[df_attacks["Country"] =="United States"]df_attacks_us = df_attacks_us[["Year", "Motive"]]df_attacks_us = df_attacks_us.dropna()temp_df = df_attacks_us[(df_attacks_us["Year"] >=2000) & (df_attacks_us["Year"] <= (2017))]motive =list(temp_df["Motive"].values)motive =" ".join(motive)from wordcloud import WordCloudimport matplotlib.pyplot as pltwordcloud = WordCloud(width=1000, height=800, background_color ='white', stopwords=stpwrd, color_func=lambda*args, **kwargs: "purple", min_font_size =10).generate(motive)plt.figure(figsize = (12, 12), facecolor =None) plt.imshow(wordcloud) plt.axis("off")plt.tight_layout(pad =2)plt.title("Attack Motives ("+str(2000) +" - "+str(2017) +")", fontdict={'fontsize': 36})plt.show()``````{python}#| label: fig-frequency#| fig-cap: "Frequency of Terrorist Attacks"yearly_freq = pd.DataFrame(df_attacks.groupby("Year")["Event ID"].count()).reset_index()yearly_freq.rename(columns={"Event ID": "Number of Terrorist Attacks"}, inplace=True)fig = px.bar(yearly_freq, x=yearly_freq["Year"], y=yearly_freq["Number of Terrorist Attacks"], title="Frequency of Terrorist Attacks (1970-2017)")fig.update_layout(title_x=0.5)fig.update_layout(height=400)fig.update_layout({'plot_bgcolor': 'rgba(0,0,0,0)','paper_bgcolor': 'rgba(0,0,0,0)'})fig.show()``````{python}#| label: fig-regions#| fig-cap: "Terrorist Attacks in different Regions"region_freq = pd.DataFrame(df_attacks.groupby(["Region", "Attack Type"])["Event ID"].count()).reset_index()region_freq.rename(columns={"Event ID": "Number of Terrorist Attacks"}, inplace=True)region_freq = region_freq.sort_values(by="Number of Terrorist Attacks", ascending=False)region_freq['Attack Type'] = region_freq['Attack Type'].replace(['Bombing/Explosion', 'Hostage Taking (Kidnapping)', 'Facility/Infrastructure Attack', 'Hostage Taking (Barricade Incident)'], ['Bombing', 'Hostage', 'Facility Attack', 'Hostage (Barr.)'])fig = px.bar(region_freq, x=region_freq["Region"], y=region_freq["Number of Terrorist Attacks"], color="Attack Type", height=400, title="Terrorist Attacks in Different Regions", barmode="relative")fig.update_layout(title_x=0.5)fig.update_layout(height=500)fig.update_layout({'plot_bgcolor': 'rgba(0,0,0,0)','paper_bgcolor': 'rgba(0,0,0,0)'})fig.show()``````{python}#| label: fig-countries#| fig-cap: "Countries with the Highest Number of Attacks"df_countries_casualties = pd.DataFrame(df_attacks.groupby(["Country"])["Casualties"].sum().reset_index())df_countries_terrorist_count = pd.DataFrame(df_attacks.groupby(["Country"])["Event ID"].count().reset_index())df_countries_terrorist_count.rename(columns={"Event ID": "Number of Terrorist Attacks"}, inplace=True)df_merged_casualties_count = df_countries_casualties.merge(df_countries_terrorist_count[["Country", "Number of Terrorist Attacks"]])df_iso_codes = px.data.gapminder()[["country", "iso_alpha"]]df_iso_codes.rename(columns={"country": "Country", "iso_alpha": "Country Code"}, inplace=True)df_iso_codes.drop_duplicates(inplace=True)df_iso_codes = df_iso_codes.reset_index()df_iso_codes.drop(["index"], axis=1, inplace=True)df_countries_terrorist_count = df_countries_terrorist_count.merge(df_iso_codes[['Country', 'Country Code']])fig = px.choropleth(df_countries_terrorist_count, locations="Country Code", color="Number of Terrorist Attacks", hover_name="Country", color_continuous_scale=px.colors.sequential.Plasma, title="Terrorist Attacks (1970 - 2017)")fig.update_layout(title_x=0.44)fig.update_layout(height=500, width=880)fig.show()``````{python}#| label: fig-targets#| fig-cap: "Common Targets of Terrorist attacks"TOP_N =11target_freq = pd.DataFrame(df_attacks.groupby("Target Type")["Event ID"].count()).reset_index()target_freq.rename(columns={"Event ID": "Number of Terrorist Attacks"}, inplace=True)rem_freq = target_freq.sort_values(by="Number of Terrorist Attacks", ascending=False)[TOP_N:]target_freq = target_freq.sort_values(by="Number of Terrorist Attacks", ascending=False)[:TOP_N]target_freq = target_freq[target_freq['Target Type'] !="Unknown"]fig = px.bar(target_freq, x='Target Type', y='Number of Terrorist Attacks', title="Common Targets of Terrorist Attacks")fig.update_layout(title_x=0.5)fig.update_layout(height=500)fig.update_layout({'plot_bgcolor': 'rgba(0,0,0,0)','paper_bgcolor': 'rgba(0,0,0,0)'})fig.show()``````{python}#| label: fig-groups#| fig-cap: "Attacks by different Terrorist Groups"groupwise_casualty_freq = pd.DataFrame(df_attacks.groupby("Terrorist Group")["Casualties"].sum()).reset_index()groupwise_casualty_freq = groupwise_casualty_freq.sort_values(by="Casualties", ascending=False)[:16]notorious_groups =list(groupwise_casualty_freq["Terrorist Group"])df_notorious_groups = df_attacks[df_attacks["Terrorist Group"].isin(notorious_groups)]df_notorious_groups = pd.DataFrame(df_notorious_groups.groupby(["Terrorist Group", "Year"])["Casualties"].sum().reset_index())df_notorious_groups["Terrorist Group"] = df_notorious_groups["Terrorist Group"].replace(["Farabundo Marti National Liberation Front (FMLN)", "Islamic State of Iraq and the Levant (ISIL)", "Kurdistan Workers' Party (PKK)", "Liberation Tigers of Tamil Eelam (LTTE)", "New People's Army (NPA)", "Nicaraguan Democratic Force (FDN)", "Revolutionary Armed Forces of Colombia (FARC)", "Shining Path (SL)", "Tehrik-i-Taliban Pakistan (TTP)"], ["Farbundo Liberation", "ISIL", "Kurdistan W.", "Tamil Tigers", "New People's Army", "Nicaraguan Force", "Colombian Force", "Shining Path", "Taliban Pakistan"])fig = px.line(df_notorious_groups, x="Year", y="Casualties", color="Terrorist Group", title='Attacks by different Terrorist Groups')fig.update_layout(title_x=0.5)fig.update_layout(height=500)fig.update_layout({'plot_bgcolor': 'rgba(0,0,0,0)','paper_bgcolor': 'rgba(0,0,0,0)'})fig.show()``````{python}#| label: fig-socioeconomic#| layout-nrow: 2#| fig-cap: "Socio-economic Aspects of Terrorist-prone Countries"#| fig-subcap: #| - "GDP"#| - "Fertility Rate"def map_region(country): region =list(df_attacks[df_attacks["Country"] == country]["Region"])[0]return regioncountry_freq = pd.DataFrame(df_attacks.groupby("Country")["Event ID"].count()).reset_index()country_freq.rename(columns={"Event ID": "Number of Terrorist Attacks"}, inplace=True)country_freq = country_freq.sort_values(by="Number of Terrorist Attacks", ascending=False)[:10]country_freq["Region"] = country_freq["Country"].apply(map_region)top_five_countries =list(country_freq["Country"].values)[:5]country_freq_year = pd.DataFrame(df_attacks.groupby(["Year", "Country"])["Event ID"].count().reset_index())country_freq_year = country_freq_year[country_freq_year["Country"].isin(top_five_countries)]country_freq_year.rename(columns={"Event ID": "Number of Terrorist Attacks"}, inplace=True)df_terrorist_gdp = df_gdp[(df_gdp["Country"].isin(top_five_countries)) & ((df_gdp["Year"] >=1970) & (df_gdp["Year"] <=2017))]df_all_gdp = df_gdp[((df_gdp["Year"] >=1970) & (df_gdp["Year"] <=2017))]df_all_gdp = df_all_gdp.dropna()df_all_gdp = pd.DataFrame(df_all_gdp.groupby("Year").mean().reset_index())df_all_gdp.rename(columns={"GDP (in USD)": "World"}, inplace=True)colorList =list(px.colors.qualitative.T10)if colorList[0] !="black": colorList.insert(0, "black")for country in top_five_countries: temp_gdp = df_terrorist_gdp[df_terrorist_gdp["Country"] == country] df_all_gdp[country] =list(temp_gdp["GDP (in USD)"])fig = px.line(df_all_gdp, x='Year', y=df_all_gdp.columns[1:], title="GDP of Terrorist-prone Countries", color_discrete_sequence=colorList, labels={"value": "GDP (in USD)","variable": "" })fig.update_layout({'plot_bgcolor': 'rgba(0,0,0,0)','paper_bgcolor': 'rgba(0,0,0,0)'})fig.update_layout(title_x=0.5)fig.update_layout(height=400, width=800)fig.show()df_all_fertility = df_population[(df_population["Year"] >=1970) & (df_population["Year"] <=2017)]df_terrorist_fertility = df_population[(df_population["Country"].isin(top_five_countries)) & ((df_population["Year"] >=1970) & (df_population["Year"] <=2017))]df_all_fertility = df_all_fertility.dropna()df_all_fertility = df_all_fertility.drop(['Migrants (net)'], axis=1)df_all_fertility = pd.DataFrame(df_all_fertility.groupby("Year").mean().reset_index())df_all_fertility.rename(columns={"Fertility Rate": "World"}, inplace=True)for country in top_five_countries: temp_fertility = df_terrorist_fertility[df_terrorist_fertility["Country"] == country] df_all_fertility[country] =list(temp_fertility["Fertility Rate"])fig = px.line(df_all_fertility, x='Year', y=df_all_fertility.columns[1:], title="Fertility Rate of Terrorist-prone Countries", color_discrete_sequence=colorList, labels={"value": "Fertility Rate","variable": "" })fig.update_layout({'plot_bgcolor': 'rgba(0,0,0,0)','paper_bgcolor': 'rgba(0,0,0,0)'})fig.update_layout(title_x=0.5)fig.update_layout(height=400, width=800)fig.show()``````{python}#| eval: falsetry:del df_attacks["Event ID"]del df_attacks["Motive"]del df_attacks["Latitude"]del df_attacks["Longitude"]except:print("Some of the columns are not present")df_attacks = df_attacks.dropna()df_attacks[['Country', 'Region', 'Province/State', 'City', 'Attack Type', 'Target Type', 'Terrorist Group', 'Weapon Type']] = df_attacks[['Country', 'Region', 'Province/State', 'City', 'Attack Type', 'Target Type', 'Terrorist Group', 'Weapon Type']].apply(LabelEncoder().fit_transform)y = df_attacks["Casualties"]X = df_attacks.drop(['Casualties'], axis=1)# Split the data into train (70%), validation (15%), and test (15%) setsX_trainval, X_test, y_trainval, y_test = train_test_split(X, y, test_size=0.15, random_state=42)X_train, X_val, y_train, y_val = train_test_split(X_trainval, y_trainval, test_size=0.20, random_state=42)scaler = RobustScaler()X_train = scaler.fit_transform(X_train)X_test = scaler.fit_transform(X_test)X_val = scaler.fit_transform(X_val)def create_bilstm(): model = Sequential() model.add(Bidirectional(LSTM(128, activation='relu', input_shape=(12,1), return_sequences=True))) model.add(Dropout(0.2)) model.add(Bidirectional(LSTM(64, activation='relu'))) model.add(Dropout(0.2)) model.add(Dense(32, activation='relu')) model.add(Dense(1))return modeldef create_ffnn(): model = Sequential() model.add(Dense(128, activation='relu', input_shape=(12,))) model.add(Dropout(0.3)) model.add(Dense(64, activation='relu')) model.add(Dropout(0.2)) model.add(Dense(32, activation='sigmoid')) model.add(Dense(16, activation='tanh')) model.add(Dense(1))return modeldef create_cnn(): model = Sequential() model.add(Conv1D(32, 3, activation='relu', input_shape=(12,1))) model.add(MaxPooling1D(2)) model.add(Conv1D(64, 3, activation='relu')) model.add(MaxPooling1D(2)) model.add(Flatten()) model.add(Dense(64, activation='relu')) model.add(Dense(1))return modeldef create_gru(): model = Sequential() model.add(GRU(64, activation='tanh', input_shape=(12,1))) model.add(Dropout(0.2)) model.add(Dense(32, activation='tanh')) model.add(Dropout(0.2)) model.add(Dense(1, activation='linear'))return modelresult = []dlModels = {"Feed Forward NN": create_ffnn(), "CNN": create_cnn(), "GRU": create_gru(), "Bi-LSTM": create_bilstm()}X_train_new = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)X_val_new = X_val.reshape(X_val.shape[0], X_val.shape[1], 1)X_test_new = X_test.reshape(X_test.shape[0], X_test.shape[1], 1)for name, model in dlModels.items(): start_time = time.time() model.compile(optimizer='adam', loss='mse')if name =="Bi-LSTM": model.fit(X_train_new, y_train, epochs=20, batch_size=128, validation_data=(X_val_new, y_val)) y_pred = model.predict(X_test_new)else: model.fit(X_train, y_train, epochs=20, batch_size=128, validation_data=(X_val, y_val)) y_pred = model.predict(X_test) result.append([name, round(np.sqrt(mean_squared_error(y_test, y_pred)), 2), round(time.time() - start_time, 2)])mlModels = {"Random Forest": RandomForestRegressor(), "K Neighbors": neighbors.KNeighborsRegressor(), "Decision Trees": DecisionTreeRegressor()}for name, model in mlModels.items(): start_time = time.time() model.fit(X_train, y_train) pred = model.predict(X_test) result.append([name, round(np.sqrt(mean_squared_error(y_test, pred)), 2), round(time.time() - start_time, 2)])pd.options.display.float_format ='{:.2f}'.formatresult_df = pd.DataFrame(result, columns=["Model", "Root Mean Squared Error", "Time (in seconds)"])result_df.to_csv("./results.csv") ``````{python}#| label: fig-results#| fig-cap: "Efficiency of Models"result_df = pd.read_csv("../results/results.csv")result_df = result_df.sort_values(by=['Root Mean Squared Error'])matplotlib.rc_file_defaults()ax1 = sns.set_style(style=None, rc=None)fig, ax1 = plt.subplots(figsize=(12,6))colors = ["#5D3FD3", "#5D3FD3", "#5D3FD3","#5D3FD3", "#0096FF", "#0096FF", "#0096FF"]sns.barplot(data = result_df, x='Model', y='Root Mean Squared Error', alpha=0.5, ax=ax1, palette=colors)ax1.set_xticklabels(ax1.get_xticklabels(), fontsize=12)ax1.set_xlabel("Models", fontsize=14)ax1.set_ylabel("Root Mean Squared Error", fontsize=14)ax1.set_title("Efficiency of Models", fontsize=16)ax2 = ax1.twinx()ax2.set_ylabel("Time (in seconds)", fontsize=14)dl = mpatches.Patch(color="#5D3FD3")ml = mpatches.Patch(color="#0096FF")custom_line = [Line2D([0], [0], color='#0096FF', lw=2), dl, ml]leg = plt.legend(custom_line, ["Time", "DL Models", "ML Models"], loc="upper left")for index, lh inenumerate(leg.legendHandles): if index >0: lh.set_alpha(0.5)sns.lineplot(data =list(result_df["Time (in seconds)"]), marker='o', ax=ax2, color='#0096FF')plt.show()```**Welcome** to *my* ~~super~~ SUPER awesome `blog` post!$$x^2 = 1$$## Quarto is coolThis section was copy/pasted from various parts of the [Quarto website](https://quarto.org/docs/get-started/hello/vscode.html).:::{.callout-note}Note that there are five types of callouts, including:`note`, `tip`, `warning`, `caution`, and `important`.::::::{.callout-tip}## Tip With CaptionThis is an example of a callout with a caption.:::For your reference, here's an example of a Python code cell in Quarto, along with a figure that gets generated, along with a caption and a label so that it can be referred to automatically as "Figure 1" (or whatever) in the writeup.For a demonstration of a line plot on a polar axis, see @fig-polar.```{python}#| label: fig-polar#| fig-cap: "A line plot on a polar axis"import numpy as npimport matplotlib.pyplot as pltr = np.arange(0, 2, 0.01)theta =2* np.pi * rfig, ax = plt.subplots( subplot_kw = {'projection': 'polar'} )ax.plot(theta, r)ax.set_rticks([0.5, 1, 1.5, 2])ax.grid(True)plt.show()```Here's an example of citing a source [see @phil99, pp. 33-35]. Be sure the source information is entered in "BibTeX" form in the `references.bib` file.The bibliography will automatically get generated. Any sources you cite in the document will be included. Other entries in the `.bib` file will not be included.